package com.twotiger.and.util;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.http.entity.StringEntity;

import android.content.Context;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.twotiger.and.Constants;
import com.twotiger.and.activity.base.BaseActivity;

/**
 * @Description: 
 *  网络请求
 *  
 * @Author douxy
 * @Date 2015年7月9日 下午1:57:37
 */
public class HttpUtil {

    public void getDate(Context mContext, HashMap<String, String> params, String path, RequestCallBack<String> callback) {
        if (!NetWorkUtil.isNetworkConnected(mContext)) {
            PromptManager.showCustomToast(mContext, "网络不可用，请检查您的网络连接");
            return;
        }
        params.put("version", "2");
        String randomNumber = RandomUtils.getRandomNumbersAndLetters(32);
        String sign = Signature.getSign(params, Constants.Appkey, randomNumber);
        LogUtil.info(HttpUtil.class, "-----签名" + sign);
        params.put("nonceStr", randomNumber);
        params.put("sign", sign);
        LogUtil.info(BaseActivity.class, path + "?" + params.toString());
        HttpUtil.doGet(mContext, path, params, callback);
    }

    public static void doGet(Context context, String url, HashMap<String, String> parm, RequestCallBack<String> callback) {
        HttpUtils client = getHttpClient(context);
        client.send(HttpMethod.GET, url, createParams(parm), callback);
    }

    public static void doPost(Context context, String url, HashMap<String, String> parm, RequestCallBack<String> callback) {
        HttpUtils client = getHttpClient(context);
        client.send(HttpMethod.POST, url, createPostParams(parm), callback);
    }

    private static RequestParams createPostParams(HashMap<String, String> parm) {
        RequestParams r = new RequestParams();
        if (MapUtils.isEmpty(parm)) {
            return r;
        }
        Set<String> keys = parm.keySet();
        for (String key : keys) {
            r.addBodyParameter(key, parm.get(key)); //此方法将参数拼接到body中
        }
        return r;
    }

    public static RequestParams createParams(HashMap<String, String> parm) {
        RequestParams r = new RequestParams();
        if (MapUtils.isEmpty(parm)) {
            return r;
        }
        Set<String> keys = parm.keySet();
        for (String key : keys) {
            r.addQueryStringParameter(key, parm.get(key)); //此方法将参数拼接到消息头
        }
        return r;
    }

    public static HttpUtils getHttpClient(Context context) {
        HttpUtils http = new HttpUtils();
        //config
        http.configUserAgent(Tools.getUserAgent(context));
        http.configCurrentHttpCacheExpiry(1000 * 10);
        http.configRequestRetryCount(2);
        http.configHttpCacheSize(0);

        return http;
    }
}
